package Andrew;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

public class RegisterMedia {

	/**
	 * The service type you want to register. The first part should be a unique
	 * name to identify your services belonging to your project. The second part
	 * identifies the transport. The last part identifies the domain. Note that
	 * this must end with a period. Service types and transports typically begin
	 * with an underscore (e.g. _tcp or _udp). Each part is separated by a
	 * period. E.g. "printer._tcp.local.".
	 */
	public static final String SERVICE_TYPE = "_media_service._tcp.local.";

	/**
	 * A unique name for the service you want to register; e.g.
	 * "casl_level3_colour".
	 */
	public static final String SERVICE_NAME = "media_a";

	/**
	 * The port on which the service will be listening.
	 */
	public static final int SERVICE_PORT = 1268;

	public static void main(final String[] args) throws IOException {
		// You can specify the interface that services are registered (and
		// browsed
		// for) on; see the commented lines following.
		final JmDNS jmdns = JmDNS.create();
		// JmDNS jmdns = JmDNS.create(InetAddress.getByName("192.168.1.102"));
		// JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());

		// Services are registered using ServiceInfo objects. Note that there
		// are
		// several create(...) methods for ServiceInfo objects. These mostly
		// differ in how you specify properties that can be associated with
		// service (the last parameter in the following line). For example, it
		// is
		// possible to provide a small hashtable which will be attached to the
		// registration and is advertised with the service info.
		final ServiceInfo info = ServiceInfo.create(RegisterMedia.SERVICE_TYPE,
				RegisterMedia.SERVICE_NAME, RegisterMedia.SERVICE_PORT, 0, 0,
				"a_property=some_value");
		jmdns.registerService(info);
		System.out.println("Registered Service as " + info);

		// The TCPSocketServer class has been modified to run as a thread
		final Thread socket = new Thread(new TestServer4(
				RegisterMedia.SERVICE_PORT));
		socket.start();
		// Wait for a keystroke before unregistering and quitting.

		System.out.println("Press enter to unregister and quit");
		new BufferedReader(new InputStreamReader(System.in)).readLine();
		// Unregister the service.
		jmdns.unregisterService(info);
		socket.stop();
		jmdns.close();
		System.exit(0);
	}
}
